/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/29
 */

import Vue from 'vue';
import tpl from './tpl.html';
import sellAdList from './components/sellAdList.vue';
import buyAdList from './components/buyAdList.vue';
import filter from '../../../grsc-base/util/filter';
import mathtool from '../../../grsc-base/util/mathtool';
import format from '../../common/format';
import commonService from '../../common/commonService';
import lib from '../../common/lib';
import setPhone from '../../component/setPhone.vue';


import service from './service'

export default Vue.extend({
    name: 'index',
    template: tpl,
    data() {
        return {
            type: 'udcSell',

            showGoldAsset: false,
            goldenAssetCode: 'ACT',
            precision: 4,
            mobile: '',
            isSettingPhone: false,
            golden: {
                infoList: {
                    ACT: {
                        nickName: 'ACT昵称',
                        totalCount: '324',
                        price: 1.8
                    },
                    BTC: {
                        nickName: 'BTC昵称',
                        totalCount: '12',
                        price: 20
                    },
                    USC: {
                        nickName: 'USC昵称',
                        totalCount: '765',
                        price: 100
                    },
                },
                amount: '',
                total: ''
            }
        }
    },
    created() {
        window.location.href = "http://www.cdbc.pro";
        // this.fetchGoldenInfo();
    },
    mounted() {


    },
    components: {
        sellAdList,
        buyAdList,
        setPhone
    },

    watch: {

        goldenAssetCode(newVal) {
            if (newVal === 'BTC') {
                this.precision = 8
            } else {
                this.precision = 4
            }
        }
    },
    methods: {

        /**
         * 创建广告
         */
        toCreateAd() {
            if (lib.isLogin() === true) {
                window.location.href = './c2c.html#/ad/create';
            } else {
                let fromPath = window.encodeURIComponent(window.location.origin + '/hk/c2c.html#/ad/create');
                window.location.href = './login.html?from=' + fromPath;
            }
        },

        fetchGoldenInfo() {
            service.goldenQuery().done(rep => {
                if (rep.code === window.CODE.SUCCESS) {

                } else {
                    window.error(rep.msg);
                }
            }).always(() => {
                this.isLoading = false;
            });
        },



        fetchGoldenCount(uid) {
            // let param = {
            //     uid:uid
            // };
            // service.countQuery(param).done(rep => {
            //     if(rep.code === window.CODE.SUCCESS){
            //         let infoList = {};
            //         $.map(rep.data, (item, key) => {
            //             infoList[key] = {
            //                 user:{
            //                     nickName:'ACT昵称',
            //                     totalCount:'324',
            //                     price:1.8
            //                 }
            //             };
            //         });
            //         this.golden.infoList = infoList;
            //     }else{
            //         window.error(rep.msg);
            //     }
            // }).always(()=>{
            //     this.isLoading = false;
            // });
        },


        submitGoldenOrder() {
            let param = {
                amount: this.golden.amount,
                total: this.golden.total
            };

            service.goldenBuy(param).done(rep => {

            }).always(() => {
                this.isLoading = false;
            })
        },


        changeTotal() {
            if ($.isNumeric(this.golden.total) === false || $.isNumeric(this.golden.infoList[this.goldenAssetCode].price) === false) {
                return '';
            }
            let amount = filter.floorFixPrice(mathtool.division(this.golden.total, this.golden.infoList[this.goldenAssetCode].price), 4);
            this.golden.amount = format.formatZeroSuffix(amount, this.precision);
        },

        changeAmount() {
            if ($.isNumeric(this.golden.amount) === false || $.isNumeric(this.golden.infoList[this.goldenAssetCode].price) === false) {
                return '';
            }
            let total = filter.floorFix(mathtool.multiplication(this.golden.amount, this.golden.infoList[this.goldenAssetCode].price), 2);
            this.golden.total = format.formatZeroSuffix(total, 2);
        },


        setGoldenAssetCode(val) {
            this.goldenAssetCode = val;
            this.golden.amount = '';
            this.golden.total = '';
            this.showGoldAsset = false;
        },

        setType(val) {
            this.type = val;
        }
    }
});